//
//  DoctorInfoController.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/21.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "DoctorInfoController.h"
#import "DoctorDTO.h"
#import "FlowerButton.h"

@interface DoctorInfoController () <UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

/*!
 *  顶部模块
 */
@property (nonatomic, strong) UIImageView *iconV;

@property (nonatomic, strong) UILabel *doctorNameLab;

@property (nonatomic, strong) UILabel *doctorClassLab;

@property (nonatomic, strong) UILabel *hospitalLab;

@property (nonatomic, strong) FlowerButton *yuyueliangBtn;
@property (nonatomic, strong) FlowerButton *flowerBtn;
@property (nonatomic, strong) FlowerButton *jinqiBtn;

//分割线
@property (nonatomic, strong) UIView *line_1;

/*!
 *  中间模块
 */
@property (nonatomic, strong) UIView *midBGView; //就诊条件和医生简介的容器
@property (nonatomic, strong) UIView *line_2; //分割两个按钮的
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;

@property (nonatomic, strong) UIButton *tempButton; //临时存储，保存信息

@property (nonatomic, strong) UIView *slideLine; //滚动下划线


@property (nonatomic, strong) UIScrollView *scrollview; //滚动

/*!
 *  新加内容，为了让同学们学到知识
 */
@property (nonatomic, strong) UITableView *tableview1; //10个cell
@property (nonatomic, strong) UITableView *tableview2;  //5个cell



/*!
 *  底部模块
 */
//咨询医生
@property (nonatomic, strong) UIButton *queryDocBtn;

@end

@implementation DoctorInfoController

static NSString *ID1 = @"cell1";
static NSString *ID2 = @"cell2";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self addLayouts];
}

- (void)setupUI{
    
    self.title = @"医生基本信息";
    
    UIButton *right = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [right setImage:[UIImage imageNamed:@"kongxin"] forState:UIControlStateNormal];
    [right setImage:[UIImage imageNamed:@"kongxin"] forState:UIControlStateHighlighted];
    [right setImage:[UIImage imageNamed:@"shixin"] forState:UIControlStateSelected];
    [right addTarget:self action:@selector(clickShiXin:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
    
    DoctorDTO *doctorDto = self.doctorDto;
    
    //1.
    self.iconV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"illness_img_person.png"]];
    self.iconV.layer.cornerRadius = 3;
    self.iconV.layer.masksToBounds = YES;
    [self.view addSubview:self.iconV];
    
    //2.
    self.doctorNameLab = [[UILabel alloc] init];
    [self.view addSubview:self.doctorNameLab];
    self.doctorNameLab.font = FONT(17);
    self.doctorNameLab.text = doctorDto.doctorName;
    
    //3.
    self.doctorClassLab = [[UILabel alloc] init];
    self.doctorClassLab.font = FONT(16);
    self.doctorClassLab.text = doctorDto.doctor_Class;
    self.doctorClassLab.textColor = LIGHT_GRAY_COLOR;
    [self.view addSubview:self.doctorClassLab];
    
    //4.
    self.hospitalLab = [[UILabel alloc] init];
    self.hospitalLab.font = FONT(17);
    self.hospitalLab.text = doctorDto.hospital;
    self.hospitalLab.textColor = LIGHT_GRAY_COLOR;
    [self.view addSubview:self.hospitalLab];
    
    
    //5.1
    self.yuyueliangBtn = [[FlowerButton alloc] init];
    [self.yuyueliangBtn setTitleColor:kThemeColor forState:UIControlStateNormal];
    [self.yuyueliangBtn setImage:[UIImage imageNamed:@"yuyueliang"] forState:UIControlStateNormal];
    self.yuyueliangBtn.userInteractionEnabled = NO;
    [self.view addSubview:self.yuyueliangBtn];
    [self.yuyueliangBtn setTitle:[NSString stringWithFormat:@"预约量:%@",doctorDto.flowerString] forState:UIControlStateNormal];
    
    //5.2
    self.flowerBtn = [[FlowerButton alloc] init];
    [self.flowerBtn setTitleColor:kThemeColor forState:UIControlStateNormal];
    [self.flowerBtn setImage:[UIImage imageNamed:@"xianhua"] forState:UIControlStateNormal];
    self.flowerBtn.userInteractionEnabled = NO;
    [self.view addSubview:self.flowerBtn];
    [self.flowerBtn setTitle:[NSString stringWithFormat:@"鲜花量:%@",doctorDto.jinqiString] forState:UIControlStateNormal];
    
    //5.3
    self.jinqiBtn = [[FlowerButton alloc] init];
    [self.jinqiBtn setTitleColor:kThemeColor forState:UIControlStateNormal];
    [self.jinqiBtn setImage:[UIImage imageNamed:@"jinqi"] forState:UIControlStateNormal];
    self.jinqiBtn.userInteractionEnabled = NO;
    [self.view addSubview:self.jinqiBtn];
    [self.jinqiBtn setTitle:[NSString stringWithFormat:@"锦旗量:%@",doctorDto.yuyueliangString] forState:UIControlStateNormal];
    
#warning  **************【 要删掉的 】**********************
    [self.yuyueliangBtn setTitle:@"10" forState:UIControlStateNormal];
    [self.flowerBtn setTitle:@"100" forState:UIControlStateNormal];
    [self.jinqiBtn setTitle:@"30" forState:UIControlStateNormal];
    
    
    //分割线
    self.line_1 = [[UIView alloc] init];
    self.line_1.backgroundColor = RGBCOLOR(224, 225, 226);
    [self.view addSubview:self.line_1];
    
    /*!
     *  中间模块
     */
    //1.背景VIew  H:50
    self.midBGView = [[UIView alloc] init];
    [self.view addSubview:self.midBGView];
    
    //2.scrollview
    
#pragma mark - scrollview 控件
    self.scrollview = [[UIScrollView alloc] init];
    self.scrollview.delegate = self;
    [self.view addSubview:self.scrollview];
    self.scrollview.contentSize = CGSizeMake(SCREEN_WIDTH*2, 100);
    self.scrollview.pagingEnabled = YES;
//    self.scrollview.backgroundColor= LIGHT_GRAY_COLOR;
    self.scrollview.showsHorizontalScrollIndicator = NO;
    
    
    self.tableview1 = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableview1.dataSource = self;
    self.tableview1.delegate = self;
    [self.scrollview addSubview:self.tableview1];
    [self.tableview1 registerClass:[UITableViewCell class] forCellReuseIdentifier:ID1];
    self.tableview1.tableFooterView = [UIView new];
    
    self.tableview2 = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableview2.dataSource = self;
    self.tableview2.delegate = self;
    [self.scrollview addSubview:self.tableview2];
    [self.tableview2 registerClass:[UITableViewCell class] forCellReuseIdentifier:ID1];
    self.tableview2.tableFooterView = [UIView new];

    
    //就诊条件
    UIButton *leftBtn = [[UIButton alloc] init];
    self.leftButton = leftBtn;
    [leftBtn setTitle:@"就诊条件" forState:UIControlStateNormal];
    [leftBtn setTitleColor:LIGHT_GRAY_COLOR forState:UIControlStateNormal];
    [leftBtn setTitleColor:kThemeColor forState:UIControlStateSelected];
    
    leftBtn.titleLabel.font = FONT(19);
    [leftBtn addTarget:self action:@selector(clickChooseBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.midBGView addSubview:self.leftButton];
    leftBtn.tag = 101;
    
    leftBtn.selected = YES;
    
    //中线
    UIView *midLine = [[UIView alloc] init];
    self.line_2 = midLine;
    midLine.backgroundColor = LIGHT_GRAY_COLOR;
    [self.midBGView addSubview:midLine];
    
    //医生简介
    UIButton *rightBtn = [[UIButton alloc] init];
    self.rightButton = rightBtn;
    [rightBtn setTitle:@"医生简介" forState:UIControlStateNormal];
    [rightBtn setTitleColor:LIGHT_GRAY_COLOR forState:UIControlStateNormal];
    [rightBtn setTitleColor:kThemeColor forState:UIControlStateSelected];
    
    rightBtn.titleLabel.font = FONT(19);
    [rightBtn addTarget:self action:@selector(clickChooseBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.midBGView addSubview:self.rightButton];
    right.tag = 102;
    
    //下划线
    self.slideLine = [[UIView alloc] init];
    self.slideLine.backgroundColor = kThemeColor;
    [self.midBGView addSubview:self.slideLine];
    
    /*!
     *  底部模块  -- 咨询医生
     */
    
    UIButton *queryDocBtn = [[UIButton alloc] init];
    [queryDocBtn setTitle:@"咨询医生" forState:UIControlStateNormal];
    queryDocBtn.titleLabel.font  = FONT(18);
    [queryDocBtn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    self.queryDocBtn = queryDocBtn;
    queryDocBtn.layer.cornerRadius = 2;
    queryDocBtn.layer.masksToBounds = YES;
    [queryDocBtn setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    [queryDocBtn setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateHighlighted];
    
    [queryDocBtn addTarget:self action:@selector(clickQueryDocBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:queryDocBtn];
    
}

-(void)addLayouts{
    
    [self.iconV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(80, 80));
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(15+64);
    }];
    
    [self.doctorNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.iconV.mas_top);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self.iconV.mas_right).offset(15);
        
    }];
    
    [self.doctorClassLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.doctorNameLab.mas_top);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self.doctorNameLab.mas_right).offset(10);
    }];
    
    [self.hospitalLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.doctorNameLab.mas_bottom).offset(10);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self.iconV.mas_right).offset(10);
    }];
    
    CGFloat btnHeight = 30;
    CGFloat btnWight  = 100;
    
    //左
    [self.yuyueliangBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.iconV.mas_bottom).offset(15);
        make.left.mas_equalTo(15);
        make.size.mas_equalTo(CGSizeMake(btnWight, btnHeight));
        
    }];
    
    //中
    [self.flowerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.yuyueliangBtn.mas_top);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(btnWight, btnHeight));
    }];
    
    //右
    [self.jinqiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.yuyueliangBtn.mas_top);
        make.right.mas_equalTo(-15);
        make.size.mas_equalTo(CGSizeMake(btnWight, btnHeight));
    }];
    
    [self.line_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(4);
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.yuyueliangBtn.mas_bottom).offset(10);
    }];
    
    /*!
     *  中间模块
     */
    [self.midBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(50);
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.line_1.mas_bottom);
    }];
    
    //中线
    [self.line_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(1);
        make.centerX.equalTo(self.midBGView);
        make.top.mas_equalTo(self.midBGView.mas_top).offset(8);
        make.bottom.mas_equalTo(self.midBGView.mas_bottom).offset(-8);
    }];
    
    //左  全部依附于父控件设置约束 不在依靠line_2
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(48);
        make.left.mas_equalTo(self.midBGView.mas_left).offset(20);
        make.right.mas_equalTo(self.line_2.mas_left).offset(-20);
        make.top.mas_equalTo(self.midBGView.mas_top).offset(0);
    }];
    
    //右
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(48);
        make.left.mas_equalTo(self.midBGView.mas_centerX).offset(20);
        make.right.mas_equalTo(self.midBGView.mas_right).offset(-20);
        make.top.mas_equalTo(self.midBGView.mas_top).offset(0);
        
    }];
    
    //下滑线
    [self.slideLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(100);
        make.centerX.mas_equalTo(self.midBGView.mas_centerX).multipliedBy(0.5);
        make.height.mas_equalTo(2);
        make.bottom.mas_equalTo(self.midBGView.mas_bottom);
    }];
    
    
#pragma mark - scrollview 约束
    [self.scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(self.midBGView.mas_bottom).offset(5);
        make.bottom.mas_equalTo(self.queryDocBtn.mas_top).offset(-10);
        make.width.mas_equalTo(SCREEN_WIDTH).multipliedBy(2);
    }];

    [self.tableview1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.scrollview.mas_left).offset(0);
        make.top.mas_equalTo(self.scrollview.mas_top).offset(0);
        
#warning  **************【 难点 】**********************
        make.bottom.mas_equalTo(self.queryDocBtn.mas_top).offset(-10);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];

    [self.tableview2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.scrollview.mas_left).offset(SCREEN_WIDTH);
        make.top.mas_equalTo(self.scrollview.mas_top).offset(0);
        make.bottom.mas_equalTo(self.queryDocBtn.mas_top).offset(-10);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    
    /*!
     *  底部模块
     */
    [self.queryDocBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(10);
        make.right.bottom.mas_equalTo(-10);
        make.height.mas_equalTo(45);
    }];
}

#pragma mark  - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{


    if (tableView == self.tableview1) {
        
        return 1;
    }else{
    
        return 5;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == self.tableview1) {
    
        return 10;
    }else{
        
        return 1;
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID1 forIndexPath:indexPath];
    
    if (tableView == self.tableview1) {
        
        cell.textLabel.text = [NSString stringWithFormat:@"就诊条件：%ld行",indexPath.row];
        
    }else{
    
        cell.textLabel.text = [NSString stringWithFormat:@"医生简介：%ld行",indexPath.section];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.tableview1) {
    
        return 44;
    }else{
    
        return 60;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
        return 0.01;
}

#pragma mark  - ScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    //1. + 1/2 宽度 除以宽度，，获取在哪一页
    CGFloat offsetX = scrollView.contentOffset.x;
    
    int a =  (offsetX+SCREEN_WIDTH*0.5)/SCREEN_WIDTH;
    
    if (a==0) {

        //2.1 设置选择状态
        self.leftButton.selected = YES;
        self.rightButton.selected = NO;
        
        //2.2 移动slideLine 和  滚动scrollview
        [UIView animateWithDuration:0.5 animations:^{
            
            self.slideLine.centerX = SCREEN_WIDTH/4;
            
        }];
        
        //4. 保存之前点击的按钮
        self.tempButton = self.leftButton;
    }else{
        
        //3.1 设置选择状态
        self.leftButton.selected = NO;
        self.rightButton.selected = YES;
        
        //3.2 移动 slideLine 和 滚动scrollview
        [UIView animateWithDuration:0.5 animations:^{
            
            self.slideLine.centerX = SCREEN_WIDTH*3/4;
        }];
        
        //4. 保存之前点击的按钮
        self.tempButton = self.rightButton;
    }
    
   
}


#pragma mark  - Actions
///选择联动按钮
- (void)clickChooseBtn:(UIButton *)btn{
    
    //1. 之前保存点击的，就退出
    if (self.tempButton == btn) {
        return;
    }

#warning  **************【 难点 】**********************

    //2.点击左边的处理事件
    if (btn == self.leftButton) {
        
        //2.1 设置选择状态
        self.leftButton.selected = YES;
        self.rightButton.selected = NO;
        
        //2.2 移动slideLine 和  滚动scrollview
        [UIView animateWithDuration:0.5 animations:^{
            
            self.slideLine.centerX = SCREEN_WIDTH/4;
            
            self.scrollview.contentOffset = CGPointMake(0, 0);
            
        }];
        
    }else{
        
        //3.1 设置选择状态
        self.leftButton.selected = NO;
        self.rightButton.selected = YES;
        
        
        //3.2 移动 slideLine 和 滚动scrollview
        [UIView animateWithDuration:0.5 animations:^{
            
            self.slideLine.centerX = SCREEN_WIDTH*3/4;
            
            self.scrollview.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
            
        }];
    }
    
    //4. 保存之前点击的按钮
    self.tempButton = btn;
}

- (void)clickShiXin:(UIButton *)btn{
    
    loggerWarn(@"点击了实心");
    
    btn.selected = !btn.isSelected;
}



-(void)clickQueryDocBtn:(UIButton *)btn{
    
    loggerWarn(@"咨询医生");
    
}


@end
